/**
 * Created by dk on 2017/5/10.
 */
var mtSearchParam = {

    "isInited": false,
    "pageSize": 10,
    "currentPage": 1,

    "shopName": "全部门店",
    "machineToolType": "",
    "orderSource": "",
    "useCachePage1": false,
    "useCachePage2": false
};


(function (doc, win) {


    var BindEvent = function () {
        //绑定下拉刷新
      /*
        refreshItem({
            "target": "#mtMgtPage .page-body",
            "callBack": function (reset) {
                mtSearchParam.currentPage = 1;
                var controller = new MTMgtController();
                controller.getMTRecords(reset);

                initAddMoreItem($("#mtMgtPage .page-body"));
            }
        });
       */
      $('#mtMgtPage .page-body .mtListContainer .list').empty();

        addMoreItem({
            "target": "#mtMgtPage .page-body",
            "callBack": function (reset) {
                mtSearchParam.currentPage++;
                var controller = new MTMgtController();
                controller.getMTRecords(reset);
            }
        });
        initAddMoreItem($("#mtMgtPage .page-body"));
    };

    if ($('#mtMgtPage').length) {
        BindEvent();
    }

    $(doc).on("pageAnimationStart", "#mtMgtPage", function () {
        BindEvent();
    });


    //1、机具列表页面
    $(doc).on("pageInit", "#mtMgtPage", function () {

        log4JS.info("进入pageInit");
        log4JS.info("原始URL：" + window.location.search);

        //初始化，当前页重置为1
        mtSearchParam.currentPage = 1;
        /* mtSearchParam.machineToolType = "";
         mtSearchParam.packageType = "";
         mtSearchParam.shopName = "";*/

        /**
         * 沙漏颜色初始化
         */
        if (mtSearchParam.useCachePage1 || mtSearchParam.useCachePage2) {
            $('#mtMgtPage .zhk-nav_filter').addClass("headIconUsed");
        } else {
            $('#mtMgtPage .zhk-nav_filter').removeClass("headIconUsed");
        }

        var controller = new MTMgtController();
        controller.getMTRecords();
    });


    $(doc).on('click', '#mtMgtPage .list li', function () {

        // var data={type:"扫码枪",store:"",versions:"",orderNum:"",logistics:"",logisticsNum:""}
        var data = {};
        data.type = $(this).find("input[name=machineToolType]").val();
        data.facilityNum = $(this).find("input[name=machineToolSn]").val();
        data.store = $(this).find("input[name=shopName]").val();
        data.versions = $(this).find("input[name=packageType]").val();
        data.orderNum = $(this).find("input[name=orderId]").val();
        data.logistics = $(this).find("input[name=logisticsName]").val();
        data.logisticsNum = $(this).find("input[name=logisticsCode]").val();

        if (data.type == '1') {
            data.type = "金员宝智能POS";
        } else if (data.type == '2') {
            data.type = "金员宝智能POS银联版";
        } else if (data.type == '3') {
            data.type = "扫码设备";
        } else if (data.type == '4') {
            data.type = "银员宝-云小盒";
        } else if (data.type == '5') {
            data.type = "小票打印机";
        } else if (data.type == '6') {
            data.type = "金员宝智慧盒子WIFI版";
        } else if (data.type == '7') {
            data.type = "金员宝智慧盒子（WIFI+4G）版";
        } else if (data.type == '8') {
            data.type = "【口碑版】POS基础版（不含卡）";
        } else if (data.type == '9') {
            data.type = "【口碑版】POS基础版（含卡）";
        } else if (data.type == '10') {
            data.type = "【口碑版】云小盒PLUS-WIFI版";
        } else if (data.type == '11') {
            data.type = "【口碑版】云小盒";
        } else if (data.type == '12') {
            data.type = "【口碑版】小票打印机";
        } else if (data.type == '13') {
            data.type = "【口碑版】扫码枪";
        } else if (data.type == '14') {
            data.type = "一年流量卡";
        } else if (data.type == '16') {
            data.type = "【口碑版】云小盒PLUS（WIFI+4G版）";
        } else {
            log4JS.error("未知机具类型：" + data.type);
            data.type = "未知机具类型";
        }


        /* if(data.versions=='1'){
             data.versions = "金员宝";
         }else if(data.versions=='2'){
             data.versions = "金员宝";
             // data.versions = "金员宝plus套餐";
         }else if(data.versions=='3'){
             data.versions = "银员宝";
         }else{
             data.versions = "未知类型";
         }
         */
        machinesDialog({ data: data })
    });


    //点击筛选
    $(doc).on('click', '#mtMgtPage .zhk-nav_filter', function () {

        // CommonUtil.injectParamObject({"XXX": "XXX"});
        // $.router.load("./machines_manage_filter.html", !mtSearchParam.useCachePage1);
        $.router.load("./machines_manage_filter.html");
    });


    //2、筛选-选择设备版本、门店
    $(doc).on("pageInit", "#mtMgtFilterPage", function () {
        log4JS.info("进入pageInit");
        log4JS.info("原始URL：" + window.location.search);

        //同步状态
        // alert(mtSearchParam.shopName);
        /* if(mtSearchParam.shopName){
              $(".store-name").html(mtSearchParam.shopName);
          }*/
        var user = sessionStorageClient.getJSON("user");
        CommonUtil.render($("#mtMgtFilter"), "mtMgtFilterTPL", { role: user.role });
        if (!mtSearchParam.useCachePage1) {
            $("li").removeClass("selected");
            $(".store-name").html("全部门店");
        } else if (mtSearchParam.shopName) {
            $(".store-name").html(mtSearchParam.shopName);
        }
        // CommonUtil.injectParamObject(mtSearchParam);

        /*
        if(mtSearchParam.machineToolType){
            $("#machineToolTypeSelect").children("li").get(mtSearchParam.machineToolType).addClass("selected");
        }*/

        if (mtSearchParam.orderSource) {
            var li = $('#mtMgtFilterPage #orderSourceSelect').children("li");
            var selectedLi = li.filter(function (ele) {
                return $(this).attr("ordersource") === String(mtSearchParam.orderSource);
            });
            selectedLi.addClass("selected");
        }
    });


    $(doc).on('click', '#mtMgtFilterPage #orderSourceSelect .filter-item', function () {

        $(this).addClass("selected").siblings().removeClass("selected");
        mtSearchParam.orderSource = $(this).attr("orderSource");
        mtSearchParam.useCachePage1 = true;
    })


    $(doc).on('click', '#mtMgtFilterPage #machineToolTypeSelect .filter-item', function () {

        $(this).addClass("selected").siblings().removeClass("selected");
        mtSearchParam.machineToolType = $(this).attr("machineToolType");
        mtSearchParam.useCachePage1 = true;
    })


    //点击重置按钮
    $(doc).on('click', '#mtMgtFilterPage .right', function () {

        $("li").removeClass("selected");
        $(".store-name").html("全部门店");
        mtSearchParam.shopName = "全部门店";
        mtSearchParam.shopId = "";
        mtSearchParam.orderSource = "";
        mtSearchParam.machineToolType = "";

        mtSearchParam.useCachePage1 = false;
        mtSearchParam.useCachePage2 = false;

    })


    $(doc).on('click', '#mtMgtFilterPage .filter-item-store', function () {
        // $.router.load("./machines_manage_filter_store_list.html", !mtSearchParam.useCachePage2);


        $.router.load("./machines_manage_filter_store_list.html");
    })


    //3、筛选-门店列表页面
    $(doc).on("pageInit", "#mtMgtShopListPage", function () {

        log4JS.info("进入pageInit");
        log4JS.info("原始URL：" + window.location.search);
        var controller = new MTMgtController();
        controller.getShopList();

        /*if(!mtSearchParam.useCachePage2){
            $("#mtMgtShopListPage li").removeClass("selected");
            $("#mtMgtShopListPage .check-all").addClass("selected");
        }*/

        // alert(mtSearchParam.shopName);

        /*if(mtSearchParam.shopName){

            var target = null;
            if(mtSearchParam.shopName=="全部门店"){
                $("#mtMgtShopListPage li").removeClass("selected");
                $("#mtMgtShopListPage .check-all").addClass("selected");
            }
            else{
                $("#mtMgtShopListPage .check-all").removeClass("selected");
                $("#mtMgtShopListPage .store-list li").each(function(){
                    if($(this).find("p").html()==mtSearchParam.shopName){
                        $(this).addClass("selected");
                    }
                })
            }
        }*/
    });


    $(doc).on('click', '#mtMgtShopListPage .store-list li', function () {

        mtSearchParam.shopName = $(this).find("p").html();
        mtSearchParam.shopId = $(this).find("p").attr("shopId");
        $("#mtMgtShopListPage .check-all").removeClass("selected");
        $("#mtMgtShopListPage .store-list li").removeClass("selected");
        $(this).addClass("selected");
        mtSearchParam.useCachePage1 = true;
        mtSearchParam.useCachePage2 = true;


        //$.router.load("./machines_manage_filter.html");
    })

    $(doc).on('click', '#mtMgtShopListPage .check-all', function () {
        mtSearchParam.shopName = $(this).html();
        mtSearchParam.shopId = '';

        $("#mtMgtShopListPage .store-list li").removeClass("selected");
        $(this).addClass("selected");
        //$.router.load("./machines_manage_filter.html");
    })


    //返回按钮
    /*$(doc).on('click','#mtMgtFilterPage .left',function(){
        $.router.back();
        // $.router.load("./machines_manage.html");
    })

    $(doc).on('click','#mtMgtShopListPage .left',function(){
        $.router.back();
        // $.router.load("./machines_manage_filter.html");
    })*/



    $(doc).on('click', '#mtMgtPage .left', function () {

        if ($(".machinesDialog").length > 0) {
            log4JS.info("关闭div");
            $(".machinesDialog").remove();
            return;
        } else {
            log4JS.info("后退");
            mtSearchParam.machineToolType = "";
            mtSearchParam.orderSource = "";
            mtSearchParam.shopId = "";
            mtSearchParam.Name="全部门店";
            mtSearchParam.useCachePage1 = false;
            mtSearchParam.useCachePage2 = false;
            $.router.back();

        }
    })



    /*var getPayUrlParam = function (key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);

        if(r && r.length > 3){
            return decodeURIComponent(r[2]);
        }else{
            log4JS.error("参数传入错误");
            DialogUtil.dialogWarn("参数传入错误");
            return null;
        }
    };*/

})(document, window);
